﻿using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace uTrade.Controls
{
    public partial class ChartPanel : DrawingRegion
    {
        public DrawingObject DrawingObjects = new DrawingObject();

        public int ScaleLineCount = 0;
        public bool IsShowTime = true;

        //背景的矩形
        private Rect rect = new Rect();

        // 数据条数，一只股票从成立到现在的天数
        private readonly Pen blackPen = new Pen(Brushes.Black, 1);

        public ChartPanel()
        {
            Load();//初始化界面，载入数据
        }

        //初始化布局，计算布局坐标
        private void InitLayout()
        {
            //Draw a Rectangle
            this.rect.Width = TotalWidth;
            this.rect.Height = TotalHeight;

            if (IsShowTime)
            {
                this.BottomFixWidth = 20;
            }
        }

        public void Load()
        {
            this.DrawingObjects.Clear();
            this.InvalidateVisual();
        }

        //显示
        protected override void OnRender(DrawingContext dc)
        {
            base.OnRender(dc);
            InitLayout();
            //绘制K线Grid
            DrawGrid(dc, ChartStartX, ChartStartY, ChartWidth, ChartHeight, ScaleLineCount);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            //this.InvalidateVisual();
        }

        //绘制线框，用途在于可视化等分图表，即上部表格，lineCount从0开始
        private void DrawGrid(DrawingContext dc, double left, double top, double width, double height, double lineCount)
        {
            //绘制竖线.
            for (double x = 0; x < width + 1; x += width / (lineCount + 1))
            {
                double xPos = (int)(left + x) + 0.5;
                dc.DrawLine(blackPen, new Point(xPos, top), new Point(xPos, height + top));
            }

            //绘制横线.
            for (double y = 0; y < height + 1; y += height / (lineCount + 1))
            {
                var yPos = (int)(top + y) + 0.5;
                dc.DrawLine(blackPen, new Point(left, yPos), new Point(width + left, yPos));
            }
        }

        //中间矩形内容框的背景色
        private void DrawBackground(DrawingContext dc)
        {
            var brush = Brushes.LightCyan;//整个区域的背景填充色
            var pen = new Pen(Brushes.LightYellow, 1);//围绕背景的线条色
            dc.DrawRectangle(brush, pen, rect);
        }
    }
}